package com.duan.common.handler;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by duanlsh on 18/6/14.
 */
//@Aspect
//@Component
@Slf4j
public class LogAspectJ {

//    StopWatch watch = null;

    @Pointcut("execution(public * com..*.controller..*(..))")
    public void log(){

    }
    String token = "";
    @Before("log()")
    public void before(JoinPoint joinPoint) {
//        watch = new StopWatch();
//        watch.start();
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();

        log.info("-->> {} 请求的url为：{}", token, request.getRequestURL());
        log.info("-->> {} 请求的method为：{}", token, request.getMethod());
        log.info("-->> {} 请求的ip为：{}", token, request.getRemoteAddr());
        log.info("-->> {} 请求的className为：{}", token, joinPoint.getSignature().getDeclaringTypeName()
                + "." + joinPoint.getSignature().getName());
        log.info("-->> {} 请求的参数为：{}", token, joinPoint.getArgs());
    }

    @After("log()")
    public void after() {
//        watch.stop();
//        log.info("-->> {} 请求时长为：{}", token, watch.getTotalTimeMillis());


    }

    @AfterReturning(pointcut = "log()", returning = "obj")
    public void afterReturning(Object obj) {
        log.info("-->> {} 请求返回的参数为：{}", token, obj.toString());
    }
}
